﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Data;
using GanttView.Charts.Common.Interface;
using System.Collections.ObjectModel;
using GanttView.Charts.Common.Instance;
using GanttView.Charts.Enum;
using GanttView.Charts.Common;
using GanttView.Charts.Common.ItemClass;
using Baosight.Platform.Core;
using System.Reflection;
using System.Collections;

namespace GanttView
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            //this.Gantt.ExpanderProvider = typeof(DynamicTaskExpander);

            //this.Gantt.IsLoading = true;
            //this.Gantt.DataSource = null;
            //this.Gantt.Update();

            //this.MouseMove+=new MouseEventHandler(MainPage_MouseMove);
        }

        private void test_Click(object sender, RoutedEventArgs e)
        {
            ////this.Gantt.IsLoading = false;
            ObservableCollection<ITaskDescriptor> tasks = new ObservableCollection<ITaskDescriptor>();
            ITaskDescriptor task = new SimpleTaskDescriptor();

            task.TaskType = TaskType.ResourceTask;
            task.ExtendAttributes = new DependencyDictionary<string, object>();


            task.TaskName = "已15,17-12030采面";
            task.StartDate = DateTime.Now;
            task.EndDate = DateTime.Now.AddDays(100);
            task.TotalMilliseconds = (task.EndDate - task.StartDate + TimeSpan.FromDays(1)).TotalMilliseconds;

            task.PercentComplete = 23;
            task.UniqueId = "111";
            task.ExtendAttributes = new DependencyDictionary<string, object>();
            task.ExtendAttributes["hh"] = "kkkkk";
            tasks.Add(task);

            this.Gantt.DataSource = tasks;
            this.Gantt.Update();

            //ObservableCollection<MyData> dataList = new ObservableCollection<MyData>();
            //dataList.Add(new MyData(1, "sss"));
            //dataList.Add(new MyData(2, "sss1"));
            //dataList.Add(new MyData(3, "sss2"));
            //dataList.Add(new MyData(4, "sss3"));

            //EIBlock block = new EIBlock("result");

            //EIColumn eiColumn = new EIColumn();
            //eiColumn.Name = "task";
            //eiColumn.TypeLabel = "C";
            //block.AddColumn(eiColumn);

            //eiColumn = new EIColumn();
            //eiColumn.Name = "startDate";
            //eiColumn.TypeLabel = "C";
            //block.AddColumn(eiColumn);

            //eiColumn = new EIColumn();
            //eiColumn.Name = "endDate";
            //eiColumn.TypeLabel = "C";
            //block.AddColumn(eiColumn);

            //IList<EIRow> _list = new List<EIRow>();
            //EIRow _map = new EIRow();
            //_map.Add("task", "已15,17-12030采面");
            //_map.Add("startDate", DateTime.Now.ToShortDateString());
            //_map.Add("endDate", DateTime.Now.AddDays(100).ToShortDateString());
            //_list.Add(_map);

            //block.AddRows(_list);


            //this.Gantt.DataSource = block;
            //this.Gantt.Update();

            //Type t = this.GetType();
            //MethodInfo m = t.GetMethod("mytest");
            //m.Invoke(this, null);
 
        }

        private void test2_Click(object sender, RoutedEventArgs e)
        {
            //ObservableCollection<ITaskDescriptor> tasks = this.Gantt.DataSource as ObservableCollection<ITaskDescriptor>;            
            //ITaskDescriptor task = new SimpleTaskDescriptor();
            //task.TaskType = TaskType.ResourceTask;            
            //tasks.Add(task);
            //task.TaskName = "已15,17-12030采面";
            //task.StartDate = DateTime.Now;
            //task.EndDate = DateTime.Now.AddDays(100);
            //task.TotalMilliseconds = (task.EndDate - task.StartDate + TimeSpan.FromDays(1)).TotalMilliseconds;

            //task.PercentComplete = 0;
            //task.UniqueId = "111";
            ////RowItemsCollection row = new RowItemsCollection();
            ////TaskEntryLayoutItem item = new TaskEntryLayoutItem(task);
            ////Gantt.TreeGrid.TreeGridLayout.Rows.Add(row);
            //Gantt.Update();

            //ObservableCollection<MyData> dataList = new ObservableCollection<MyData>();
            //dataList.Add(new MyData(1, "sss"));
            //dataList.Add(new MyData(2, "sss1"));
            //dataList.Add(new MyData(3, "sss2"));
            //dataList.Add(new MyData(4, "sss3"));

            //EIBlock block = new EIBlock("result");

            //EIColumn eiColumn = new EIColumn();
            //eiColumn.Name = "task";
            //eiColumn.TypeLabel = "C";
            //block.AddColumn(eiColumn);

            //eiColumn = new EIColumn();
            //eiColumn.Name = "startDate";
            //eiColumn.TypeLabel = "C";
            //block.AddColumn(eiColumn);

            //eiColumn = new EIColumn();
            //eiColumn.Name = "endDate";
            //eiColumn.TypeLabel = "C";
            //block.AddColumn(eiColumn);

            //IList<EIRow> _list = new List<EIRow>();
            //EIRow _map = new EIRow();
            //_map.Add("task", "已15,17-12030采面");
            //_map.Add("startDate", DateTime.Now.ToShortDateString());
            //_map.Add("endDate", DateTime.Now.AddDays(100).ToShortDateString());
            //_list.Add(_map);

            //block.AddRows(_list);
            //this.dg.ItemsSource = dataList;

            //EIBlock block = this.Gantt.DataSource as EIBlock;
            //EIRow row = new EIRow();
            //block.AddRow(row);
            //this.Gantt.Update();   

            TaskEntryLayoutItem task = this.Gantt.TreeGrid.SelectedItems[0] as TaskEntryLayoutItem;

            ObservableCollection<ITaskDescriptor> tasks = this.Gantt.DataSource as ObservableCollection<ITaskDescriptor>;
            tasks.Remove(task.Data);
            this.Gantt.Update();
        }

        private void myCombox_Loaded(object sender, RoutedEventArgs e)
        {
            ObservableCollection<MyData> dataList = new ObservableCollection<MyData>();
            dataList.Add(new MyData(1, "sss"));
            dataList.Add(new MyData(2, "sss1"));
            dataList.Add(new MyData(3, "sss2"));
            dataList.Add(new MyData(4, "sss3"));
            ComboBox box = sender as ComboBox;
            box.ItemsSource = dataList;
            DataGridCell o = box.Parent as DataGridCell;
            //box.SelectedItem = (from q in dataList where (q as MyData).Text == (o.DataContext as MyData).Text select q).FirstOrDefault();
        }

        public void mytest()
        {

            EIBlock block = new EIBlock("result");

            EIColumn eiColumn = new EIColumn();
            eiColumn.Name = "task";
            eiColumn.TypeLabel = "C";
            block.AddColumn(eiColumn);

            eiColumn = new EIColumn();
            eiColumn.Name = "startDate";
            eiColumn.TypeLabel = "C";
            block.AddColumn(eiColumn);

            eiColumn = new EIColumn();
            eiColumn.Name = "endDate";
            eiColumn.TypeLabel = "C";
            block.AddColumn(eiColumn);

            IList<EIRow> _list = new List<EIRow>();
            EIRow _map = new EIRow();
            _map.Add("task", "已15,17-12030采面");
            _map.Add("startDate", "2013-08-12");
            _map.Add("endDate", "2013-12-22");
            _list.Add(_map);

            block.AddRows(_list);


            this.Gantt.DataSource = block;
            this.Gantt.Update();
        }
    }

    public class MyData
    {
        public MyData(int id,string text)
        {
            this.Id = id;
            this.Text = text;
        }
            
        private int id;

        public int Id
        {
            get { return id; }
            set { id = value; }
        }
        private string text;

        public string Text
        {
            get { return text; }
            set { text = value; }
        }
    }

    public class RequireTimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double time;
            if (double.TryParse(value.ToString(), out time))
            {
                if (parameter == null)
                    parameter = "天";
                switch (parameter.ToString())
                {
                    case "天":
                        return (TimeSpan.FromMilliseconds(time)).TotalDays;
                }

            }
            return value;
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            double time;
            if (double.TryParse(value.ToString(), out time))
            {
                if (parameter == null)
                    parameter = "天";
                switch (parameter.ToString())
                {
                    case "天":
                        return TimeSpan.FromDays(time).TotalMilliseconds;
                }

            }
            return value;
        }
    }


    public class DateTimeConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            DateTime dt = (DateTime)value;
            if (dt == DateTime.MinValue)
                return "";
            return dt.ToString("yyyy/M/d");
        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            string dt = value as string;
            if(dt==null)
                return DateTime.Now;
            return DateTime.Parse(dt);
        }
    }
}
